name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - run: cargo fmt --check

    - run: cargo build --verbose
    - run: cargo test --verbose
    - run: cargo doc

    # host: x86_64-unknown-linux-gnu
    - run: cargo build --release

    - run: |
        tar --zstd -cvf librush_release_x86_64-unknown-linux-gnu.tar.zst \
        target/release/ibrus
    - uses: actions/upload-artifact@v4
      with:
        name: librush_release_x86_64-unknown-linux-gnu
        path: librush_release_x86_64-unknown-linux-gnu.tar.zst

    # target: aarch64-unknown-linux-gnu
    - run: |
        sudo apt-get update && \
        sudo apt-get -y install \
        binutils-aarch64-linux-gnu \
        gcc-aarch64-linux-gnu \
        g++-aarch64-linux-gnu

    - run: rustup target add aarch64-unknown-linux-gnu

    - run: cargo build --release --target aarch64-unknown-linux-gnu
      env:
        CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
        CC_aarch64_unknown_linux_gnu: aarch64-linux-gnu-gcc
        CXX_aarch64_unknown_linux_gnu: aarch64-linux-gnu-g++

    - run: |
        tar --zstd -cvf librush_release_aarch64-unknown-linux-gnu.tar.zst \
        target/aarch64-unknown-linux-gnu/release/ibrus
    - uses: actions/upload-artifact@v4
      with:
        name: librush_release_aarch64-unknown-linux-gnu
        path: librush_release_aarch64-unknown-linux-gnu.tar.zst

    # build rpm (fedora toolbox rpm-build)
    - run: sudo apt-get -y install podman-toolbox
    # Fedora 40
    - run: toolbox create -y -d fedora -r 40
      env:
        SHELL: /usr/bin/bash
    - run: |
        toolbox run -c fedora-toolbox-40 \
        sudo dnf install -y rpm-build rpm-devel rpmdevtools
      env:
        SHELL: /usr/bin/bash
    - run: |
        toolbox run -c fedora-toolbox-40 \
        rpmdev-setuptree
      env:
        SHELL: /usr/bin/bash
    # copy files
    - run: |
        cp rpm/ibrus.spec ~/rpmbuild/SPECS/ && \
        cp aur/pmim_ibrus.xml ~/rpmbuild/SOURCES/ && \
        cp target/release/ibrus ~/rpmbuild/SOURCES/
    # build rpm
    - run: |
        toolbox run -c fedora-toolbox-40 \
        rpmbuild -bb ~/rpmbuild/SPECS/ibrus.spec
      env:
        SHELL: /usr/bin/bash

    - uses: actions/upload-artifact@v4
      with:
        name: ibrus-0.1.0-1.fc40.x86_64.rpm
        path: ~/rpmbuild/RPMS/x86_64/ibrus-0.1.0-1.fc40.x86_64.rpm

    # Fedora 39
    - run: toolbox create -y -d fedora -r 39
      env:
        SHELL: /usr/bin/bash
    - run: |
        toolbox run -c fedora-toolbox-39 \
        sudo dnf install -y rpm-build rpm-devel rpmdevtools
      env:
        SHELL: /usr/bin/bash
    - run: |
        toolbox run -c fedora-toolbox-39 \
        rpmbuild -bb ~/rpmbuild/SPECS/ibrus.spec
      env:
        SHELL: /usr/bin/bash

    - uses: actions/upload-artifact@v4
      with:
        name: ibrus-0.1.0-1.fc39.x86_64.rpm
        path: ~/rpmbuild/RPMS/x86_64/ibrus-0.1.0-1.fc39.x86_64.rpm
